const int Max = 10;
int edge[Max+1][Max+1];
int dist[Max];
int visited[Max];
void prim()
{
    //初始化 邻接矩阵 无穷等于999
  int InitMax1 = Max;
  int InitMax2 = Max;
  while(InitMax1--)
  {
    InitMax2 = Max;
    while(InitMax2--)
      edge[InitMax1][InitMax2] = 999;
  }
    //初始化dist数组 给矩阵赋值
  int Cost = 0;
  int x1,x2;
  int n;
  cin>>n;
  while(true)
  {
    cin>>x1;
    if(x1<=0) break;
    cin>>x2;
    cin>>edge[x1][x2];
  }
  for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=n;j++){cout<<edge[i][j]<<" " ;}
        cout<<endl;
    }
  visited[1] = 1;
  int i;
  for(i=1;i<=n;i++)
  {
    dist[i] = edge[1][i];
    cout<<dist[i]<<" ";
  }
    //循环n-1 构造最小生成树
  for(i = 1;i<=n-1;i++)
  {
    int j = 1;
    int MinCost=999;
    int MinIndex;
    while(j<=n)
    {
      if(dist[j]<MinCost&&visited[j]==0)
      {
        MinCost = dist[j];
        MinIndex = j;

      }
      j++;
    }
    Cost += MinCost;
    visited[MinIndex] = 1;
    j=1;
        //更新dist数组
    while(j<=n)
    {
      if(dist[j]>edge[MinIndex][j])
      {
        dist[j] = edge[MinIndex][j];
      }
      j++;
    }
  }
  cout<<endl<<Cost<<endl;
}